package us.icitap.commandcenter.ui.dispatcher;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Call;
import us.icitap.commandcenter.business.ejb.persistence.CallManagement;
import us.icitap.commandcenter.business.ejb.persistence.Dispatcher;
import us.icitap.commandcenter.listeners.ChangeDispatcherListener;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustComboBox;
import us.icitap.commandcenter.ui.custom.CustSaveButton;
import us.icitap.commandcenter.ui.custom.CustTextArea;
import us.icitap.commandcenter.utils.HandleNotification;

import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button.ClickEvent;

public class ChangeDispatcherWindow extends Window implements ClickListener{

	ChangeDispatcherListener changeDispatcherListener;
	Call call;
	
	private CustComboBox dispatcherComboBox = new CustComboBox("Zona e mbulimit");
	private CustTextArea notesTextArea = new CustTextArea("Shenime mbi ndryshimin e zones se mbulimit");
	private final CustSaveButton saveButton = new CustSaveButton("RUAJ");
	
	public ChangeDispatcherWindow(Call call, ChangeDispatcherListener changeDispatcherListener) {
		this.call = call;
		this.changeDispatcherListener = changeDispatcherListener;
		this.center();
		this.setModal(true);
		this.setResizable(false);
		
		VerticalLayout vl = new VerticalLayout();
		vl.setMargin(true);
		
		List<Dispatcher> dispatchers = CommandCenterUI.getUser().getCommandCenter().getActiveDispatcherList();	
		dispatcherComboBox.setRequired(true);
		dispatcherComboBox.setRequiredError("Duhet te plotesoni zonen e mbulimit");
		dispatcherComboBox.setContainerDataSource(new BeanItemContainer<Dispatcher>(Dispatcher.class, dispatchers));
		dispatcherComboBox.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		dispatcherComboBox.setItemCaptionPropertyId("name");
		dispatcherComboBox.setImmediate(true);
		vl.addComponent(dispatcherComboBox);
		
		this.notesTextArea.setWidth("300px");
		vl.addComponent(notesTextArea);
		
		final ButtonsLayout buttonsLayout = new ButtonsLayout("500px");
		buttonsLayout.addComponent(saveButton);
		buttonsLayout.setComponentAlignment(saveButton, Alignment.MIDDLE_RIGHT);
		vl.addComponent(buttonsLayout);
		
		saveButton.addClickListener(this);

		
		this.setContent(vl);
	}

	
	public void setCall(Call call){
		this.call = call;
		this.dispatcherComboBox.setValue(null);
		this.notesTextArea.setValue(null);
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		try {
			
			
			dispatcherComboBox.commit();
			notesTextArea.commit();
			call.setPreviousDispatcher(call.getMainDispatcher());
			call.setMainDispatcher((Dispatcher) dispatcherComboBox.getValue());
			
			CallManagement callManagement = new CallManagement(call);
			if(notesTextArea.getValue() == null || notesTextArea.getValue().trim().equals(""))
				callManagement.setNotes("U ndryshua zona e mbulimit nga " + call.getPreviousDispatcher().getName() + " ne " + call.getMainDispatcher().getName());
			
			callManagement.setRecordDate(new Date());
			callManagement.setRecordUser(CommandCenterUI.getUser().getUsername());
			
			if(call.getCallManagementList() == null)
				call.setCallManagementList(new ArrayList<CallManagement>());
			call.getCallManagementList().add(callManagement);
		
			
			changeDispatcherListener.changeDispatcher(call);
			
		} catch (Exception e) {
			HandleNotification.showWarningOnScreen(e.getCause().getMessage());
		}
		
	}
}
